package com.wiloon.java8x.io;

/*
  Created by roy.wang on 2017/4/20;
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

public class FileStreamDemo {
    public static void main(String[] args) throws URISyntaxException {
        try {
            URL from = ClassLoader.getSystemClassLoader().getResource("foo.txt");
            File srcFile = new File(from.toURI());
            URL to = ClassLoader.getSystemClassLoader().getResource("bar.txt");
            File desFile = new File(to.toURI());


            byte[] buffer = new byte[1024];
            FileInputStream fileInputStream = new FileInputStream(srcFile);
            FileOutputStream fileOutputStream = new FileOutputStream(desFile);

            // available()可取得未读取的数据长度
            System.out.println("复制文件：" + fileInputStream.available() + "字节");

            while (true) {
                if (fileInputStream.available() < 1024) {
                    // 剩余的数据比1024字节少
                    // 一位一位读出再写入目的文件
                    int remain = -1;
                    while ((remain = fileInputStream.read()) != -1) {
                        fileOutputStream.write(remain);
                    }
                    break;
                } else {
                    // 从来源文件读取数据至缓冲区
                    fileInputStream.read(buffer);
                    // 将数组数据写入目的文件
                    fileOutputStream.write(buffer);
                }
            }
            // 关闭流
            fileInputStream.close();
            fileOutputStream.close();
            System.out.println("复制完成");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("using: java FileStreamDemo src des");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}